package cm.mefobe.android.telecomcost.entities;

import java.io.InvalidObjectException;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Calendar;

import android.text.format.DateUtils;

public class Month extends DayRange {
	private int _monthOfYear;
	private int _year;

	public Month() {
	}

	public ArrayList<Week> getWeeks() throws InvalidObjectException {
		ArrayList<Week> weeks = new ArrayList<Week>();

		Day firstDayOfWeek1 = new Day(1, getMonthOfYear(), getYear());
		Day lastDayOfWeek1 = firstDayOfWeek1;

		while (!lastDayOfWeek1.getName().equals(
				DateUtils.getDayOfWeekString(Calendar.SUNDAY,
						DateUtils.LENGTH_LONG))) {
			lastDayOfWeek1 = new Day(lastDayOfWeek1.getDayOfWeek() + 1,
					getMonthOfYear(), getYear());
		}

		Week week1 = new Week(firstDayOfWeek1, lastDayOfWeek1);
		weeks.add(week1);

		Day firstDayOfWeek2 = new Day(lastDayOfWeek1.getDayOfMonth() + 1,
				getMonthOfYear(), getYear());
		if (!firstDayOfWeek2.getName().equals(
				DateUtils.getDayOfWeekString(Calendar.MONDAY,
						DateUtils.LENGTH_LONG))) {
			throw new InvalidObjectException(
					"could not calculate the last day of first week of "
							+ getName());
		}

		Day lastDayOfWeek2 = firstDayOfWeek2;
		while (!lastDayOfWeek2.getName().equals(
				DateUtils.getDayOfWeekString(Calendar.SUNDAY,
						DateUtils.LENGTH_LONG))) {
			lastDayOfWeek2 = new Day(lastDayOfWeek2.getDayOfMonth() + 1,
					getMonthOfYear(), getYear());
		}

		Week week2 = new Week(firstDayOfWeek2, lastDayOfWeek2);
		weeks.add(week2);

		Day firstDayOfWeek3 = new Day(lastDayOfWeek2.getDayOfMonth() + 1,
				getMonthOfYear(), getYear());
		if (!firstDayOfWeek3.getName().equals(
				DateUtils.getDayOfWeekString(Calendar.MONDAY,
						DateUtils.LENGTH_LONG))) {
			throw new InvalidObjectException(
					"could not calculate the last day of 2nd week of "
							+ getName());
		}

		Day lastDayOfWeek3 = firstDayOfWeek3;
		while (!lastDayOfWeek3.getName().equals(
				DateUtils.getDayOfWeekString(Calendar.SUNDAY,
						DateUtils.LENGTH_LONG))) {
			lastDayOfWeek3 = new Day(lastDayOfWeek3.getDayOfMonth() + 1,
					getMonthOfYear(), getYear());
		}

		Week week3 = new Week(firstDayOfWeek3, lastDayOfWeek3);
		weeks.add(week3);

		Day firstDayOfWeek4 = new Day(lastDayOfWeek3.getDayOfMonth() + 1,
				getMonthOfYear(), getYear());
		if (!firstDayOfWeek4.getName().equals(
				DateUtils.getDayOfWeekString(Calendar.MONDAY,
						DateUtils.LENGTH_LONG))) {
			throw new InvalidObjectException(
					"could not calculate the last day of 3rd week of "
							+ getName());
		}

		Day lastDayOfWeek4 = firstDayOfWeek4;
		while (!lastDayOfWeek4.getName().equals(
				DateUtils.getDayOfWeekString(Calendar.SUNDAY,
						DateUtils.LENGTH_LONG))) {
			lastDayOfWeek4 = new Day(lastDayOfWeek4.getDayOfMonth() + 1,
					getMonthOfYear(), getYear());
		}

		Week week4 = new Week(firstDayOfWeek4, lastDayOfWeek4);
		weeks.add(week4);

		if (!getName().equals(
				DateUtils.getMonthString(Calendar.FEBRUARY,
						DateUtils.LENGTH_LONG))) {
			Day firstDayOfWeek5 = new Day(lastDayOfWeek4.getDayOfMonth() + 1,
					getMonthOfYear(), getYear());
			if (!firstDayOfWeek5.getName().equals(
					DateUtils.getDayOfWeekString(Calendar.MONDAY,
							DateUtils.LENGTH_LONG))) {
				throw new InvalidObjectException(
						"could not calculate the last day of 4th week of "
								+ getName());
			}

			Day lastDayOfWeek5 = new Day(getMaxDays(), getMonthOfYear(),
					getYear());

			Week week5 = new Week(firstDayOfWeek5, lastDayOfWeek5);
			weeks.add(week5);
		}

		return weeks;
	}

	public int getMaxDays() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getFirstDay().getDate());
		return cal.getMaximum(Calendar.DAY_OF_MONTH);
	}
	
	@Override
	public ArrayList<Day> getDays()
	{
		ArrayList<Day> weekDays = new ArrayList<Day>();
		Day firstDay = getFirstDay();
		
		if(firstDay != null)
		{
			while(firstDay.getDayOfMonth() <= getLastDay().getDayOfMonth())
			{
				weekDays.add(firstDay);
				firstDay = new Day(firstDay.getDayOfMonth() + 1,firstDay.getMonthOfYear(),firstDay.getYear());
			}
			
			return weekDays;
		}
		
		throw new InvalidParameterException("cannot get the first day of this day range");
	}

	@Override
	public Day getFirstDay() {
		return new Day(1, getMonthOfYear(), getYear());
	}

	@Override
	public Day getLastDay() {
		return new Day(getMaxDays(), getMonthOfYear(), getYear());
	}

	public Month(int monthOfYear) {
		setMonthOfYear(monthOfYear);
	}

	public void setMonthOfYear(int _monthOfYear) {
		this._monthOfYear = _monthOfYear;
	}

	public int getMonthOfYear() {
		return _monthOfYear;
	}

	public void setYear(int Year) {
		_year = Year;
	}

	public int getYear() {
		if (_year == 0) {
			Calendar cal = Calendar.getInstance();
			_year = cal.get(Calendar.MONTH);
		}

		return _year;
	}

	@Override
	public String toString() {
		return getName();
	}

	@Override
	public long getId() {
		return _monthOfYear;
	}

	@Override
	public void setName(String name) {
	}

	@Override
	public void setId(long _id) {
	}

	@Override
	public String getName() {
		return DateUtils.getMonthString(_monthOfYear, DateUtils.LENGTH_LONG);
	}
	
	public String getShortestName() {
		return DateUtils.getMonthString(_monthOfYear, DateUtils.LENGTH_SHORTEST);
	}
	
	public String getShortName() {
		return DateUtils.getMonthString(_monthOfYear, DateUtils.LENGTH_SHORT);
	}
}